package com.industrial.system.controller;

import com.industrial.common.core.utils.MD5Utils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.common.security.utils.DictUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.domain.SysRole;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.service.ISysAdminPersManagService;
import com.industrial.system.service.ISysPostService;
import com.industrial.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin")
public class SysAdminPersManagController extends BaseController {

    @Autowired
    private ISysAdminPersManagService sysAdminPersManagService;
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    @GetMapping("/list")
    public TableDataInfo list(SysUser user)
    {

        startPage();
        List<SysUser> list = sysAdminPersManagService.selectUserList(user);
        return getDataTable(list);
    }

    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) String userId)
    {
        sysAdminPersManagService.checkUserDataScope(userId);
        AjaxResult ajax = AjaxResult.success();
        List<SysRole> roles = roleService.selectRoleAll();
        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(userId))
        {
            SysUser sysUser = sysAdminPersManagService.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, sysUser);
            ajax.put("postIds", postService.selectPostListByUserId(userId));
            ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        }
        return ajax;
    }

    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysUser user)
    {
        sysAdminPersManagService.checkUserAllowed(user);
        sysAdminPersManagService.checkUserDataScope(user.getUserId());
        if (!sysAdminPersManagService.checkUserNameUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        user.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysAdminPersManagService.updateUser(user));
    }


    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysUser user)
    {
        if (!sysAdminPersManagService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        user.setCreateBy(SecurityUtils.getUsername());
//        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setPassword(MD5Utils.encrypt(user.getUserName(),user.getPassword()));
        return toAjax(sysAdminPersManagService.insertUser(user));
    }

    @DeleteMapping("/del/{userId}")
    public AjaxResult remove(@PathVariable("userId") String userId){
        if (StringUtils.equals(userId, SecurityUtils.getUserId()))
        {
            return error("当前用户不能删除");
        }
        return toAjax(sysAdminPersManagService.deleteUserById(userId));
    }
}
